package com.xm.xmapp;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;
import com.xm.xmapp.adapter.MyPagerAdapters;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.YHQTipDialog;
import com.xm.xmapp.fragment.YHQFragment;
import com.xm.xmapp.response.bean.ResponseUserCouponsList;
import com.xm.xmapp.tools.AndroidTool;
import com.xm.xmapp.tools.Tools;

public class YHQActivity extends BaseActivity implements OnClickListener {

	private ViewPager vp;
	private TabLayout tab;
	private MyPagerAdapters mAdapter;
	private ImageView right;
	private TextView right_title;
	private ResponseUserCouponsList bean;// 选择使用优惠券
	private int flag;// flag==0表示展示==1表示选择使用优惠券

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_yhq);

	}

	@Override
	public void initView() {

		vp = (ViewPager) findViewById(R.id.vp);
		tab = (TabLayout) findViewById(R.id.tab);
		right_title = (TextView) findViewById(R.id.right_title);
		right = (ImageView) findViewById(R.id.right);
	}

	@Override
	public void initData() {
		right_title.setVisibility(View.GONE);
		// 实例化适配器
		setMyTitle("优惠券");
		intent = getIntent();
		flag = getIntent().getIntExtra("flag", 0);
		if (flag == 0) {
			/**
			 * 可用，2=不可用，3=已使用
			 */
			// right_title.setVisibility(View.VISIBLE);
			right.setVisibility(View.VISIBLE);
			right.setImageResource(R.mipmap.coupon_icon_question);
			String[] title = { "可    用", "不可用", "已使用" };
			mAdapter = new MyPagerAdapters(title, getSupportFragmentManager(), new ArrayList<Fragment>());

			YHQFragment availableFragment = new YHQFragment();
			availableFragment.setData(1, flag, null, null);

			YHQFragment unusableFragment = new YHQFragment();
			unusableFragment.setData(2, flag, null, null);

			YHQFragment haveUsableFragment = new YHQFragment();
			haveUsableFragment.setData(2, flag, null, null);

			mAdapter.fragmentsList.add(availableFragment);
			mAdapter.fragmentsList.add(unusableFragment);
			mAdapter.fragmentsList.add(haveUsableFragment);

		} else {

			/**
			 * 可用，2=不可用，3=已使用
			 */
			right.setVisibility(View.GONE);
			right_title.setVisibility(View.VISIBLE);
			right_title.setText("确定");
			String[] title = { "可    用", "不可用" };
			mAdapter = new MyPagerAdapters(title, getSupportFragmentManager(), new ArrayList<Fragment>());
			ResponseUserCouponsList bean = (ResponseUserCouponsList) getIntent().getSerializableExtra("bean");
			double money = getIntent().getDoubleExtra("money", 0);
			String moneyParm = null;
			if (money >= 0)
				moneyParm = money + "";
			this.bean = bean;

			YHQFragment availableFragment = new YHQFragment();
			availableFragment.setData(1, flag, bean, moneyParm);

			YHQFragment unusableFragment = new YHQFragment();
			unusableFragment.setData(2, flag, bean, moneyParm);

			mAdapter.fragmentsList.add(availableFragment);
			mAdapter.fragmentsList.add(unusableFragment);

		}


		// 设置Viewpage属性
		vp.setAdapter(mAdapter);
		vp.setOffscreenPageLimit(3);
		// 关联ViewPage
		tab.setupWithViewPager(vp);
		tab.post(new Runnable() {
			@Override
			public void run() {
				int marig = Tools.dp2px(10);
				AndroidTool.setIndicator(tab,marig,marig);
			}
		});
	}

	@Override
	public void initEvent() {

		right.setOnClickListener(this);
		right_title.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {

		switch (arg0.getId()) {

		case R.id.right_title:

		case R.id.right:
			if (flag == 0) {
				YHQTipDialog yHQTipDialog = new YHQTipDialog(this);
				yHQTipDialog.show();
			} else {
				intent.putExtra("bean", bean);
				setResult(ShortTimeOrderPayActivity.YHQCode, intent);
				finish();
			}

			break;

		default:
			break;
		}
	}

	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case Type.A_YHQ.SECELT_YHQ:
			if (data != null)
				bean = (ResponseUserCouponsList) data;
			else
				bean = null;
			break;

		default:
			break;
		}

		return super.dataChang(flag, data);
	}

}
